//
//  GeneralLayer.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 14/8/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "GeneralLayer.h"
#import "SquareSprite.h"
#import "MotorSprite.h"
#import "LanternSprite.h"

@implementation GeneralLayer
@synthesize delegate;
@synthesize currentWave;
@synthesize spritesArray;

-(id) init
{
    if( (self = [super initWithColor:ccc4(255, 255, 255, 255)]) )
    {		
		//Allow Touch
		self.isTouchEnabled = YES;
		
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		[self setContentSize:CGSizeMake(winSize.width * GENERAL_LAYER_X_RATIO, winSize.height * GENERAL_LAYER_Y_RATIO)];
    }
        
    return self;
}

-(void) dealloc
{
	[spritesArray release];
    [super dealloc];
}

-(void) showGuGu:(CGPoint)position
{
    CCSprite *guguSprite = [CCSprite spriteWithFile:@"Icon@2x.png"];
    [guguSprite setPosition:position];
    [guguSprite setScale:0];
    [self addChild:guguSprite z:999];
    
    CCScaleTo *scaleAction = [CCScaleTo actionWithDuration:2 scale:1];
    CCMoveTo *moveAction = [CCMoveTo actionWithDuration:2 position:ccp(self.contentSize.width/2, self.contentSize.height/2)];
    CCCallFunc *func = [CCCallFunc actionWithTarget:delegate selector:@selector(levelCompleted)];
    
    [guguSprite runAction:[CCSequence actions: [CCSpawn actions:scaleAction, moveAction, nil], func, nil]]; 
}


-(GeneralSprite *) createSpriteWithType:(ESpriteType)type number:(int)num
{    
    GeneralSprite *sprite;
    
    if(type == kSPRITE_TYPE_SQUARE)
    {
        //Create the Sprite
        sprite = (SquareSprite*)[SquareSprite spriteWithFile:@"Icon@2x.png"];
		[sprite setScaleY:0.6f];
		[sprite setScaleX:0.7f];
        [sprite.numberLabel setPosition:ccp(sprite.contentSize.width/2, sprite.contentSize.height/2)];  //Set the number position
    }
    else if(type == kSPRITE_TYPE_MOTOR)
    {
        sprite = (MotorSprite *)[MotorSprite spriteWithFile:@"gugu_sheep.png"];
        [sprite.numberLabel setPosition:ccp( sprite.contentSize.width/2, sprite.contentSize.height * 2/3 )];
    }
    else if(type == kSPRITE_TYPE_LANTERN)
    {
        sprite = (LanternSprite *)[LanternSprite spriteWithFile:@"Icon@2x.png"];
        [sprite.numberLabel setPosition:ccp(sprite.contentSize.width/2,
                                            sprite.contentSize.height/2)];
    }
    else
    {
        CCLOG(@"Unknow Sprite Type");
        return nil;
    }
    
    NSString *strNumber = [NSString stringWithFormat:@"%d", num];
    
    [sprite setNumber:num]; //init the number belonging to this sprite
    [sprite.numberLabel setColor:ccc3(255, 0, 0)];    //Set color of the number
    [sprite.numberLabel setString:strNumber];   //Set the value of the number
	
    return sprite;
}

-(NSMutableArray *) shuffleArray:(NSMutableArray *)array
{
	static BOOL seeded = NO;
	if(!seeded)
	{
		seeded = YES;
		srandom(time(NULL));
	}
	
    NSUInteger count = [array count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        NSUInteger n = (random() % nElements) + i;
		[array exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
	

	return array;
}

#pragma mark GernalSpriteDelegate
-(void) completeCorrectEffect:(GeneralSprite *)sprite
{
	//Tell GeneralScene sprite action completed
	[delegate completePerformCorrectAnswer:sprite];
}

-(void) completeIncorrectEffect:(GeneralSprite *)sprite
{
	//Tell GeneralScene sprite action completed
	[delegate completePerformIncorrectAnswer:sprite];
}

	
#pragma mark Abstract Function
-(void) createSprites:(NSMutableArray *)numberArray wave:(int)wave
{
    CCLOG(@"createSprites Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) startLevel
{
    CCLOG(@"startLevel Should be inherited");
    [self doesNotRecognizeSelector:_cmd];

}

-(void) startWave:(int)wave
{
    CCLOG(@"startWave Should be inherited");
    [self doesNotRecognizeSelector:_cmd];

}

-(void) willCompleteLevel
{
	CCLOG(@"willCompleteLevel Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) willCompleteWave
{
    CCLOG(@"willCompleteWave Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) startPerformCorrectAnswer:(GeneralSprite *)sprite
{
    CCLOG(@"startPerformCorrectAnswer Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) startPerformIncorrectAnswer:(GeneralSprite *)sprite
{
    CCLOG(@"startPerformIncorrectAnswer Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CCLOG(@"ccTouchesBegan Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CCLOG(@"ccTouchesEnded Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self ccTouchesEnded:touches withEvent:event];
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CCLOG(@"ccTouchesMoved Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

@end


